/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.Audio.RSAudio;
import FrameWork.IPlay;
import FrameWork.IState;
import FrameWork.Point;
import FrameWork.RSFont;
import FrameWork.Rectangle;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author DANTRUONG
 */
public class State_Start extends IState{
    int m_Status;
    float m_TimeUpdate;
    Image m_Logo;
    int[] m_Raw;
    int m_Alpha;
    Rectangle[] m_Rect = new Rectangle[2];
    
    public State_Start(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_START ;
    }
    
    public void Init (){
        super.Init();
        m_Status = 0;
        try {
            m_Logo = Image.createImage("/Image/Logo.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        m_Rect[0] = new Rectangle(5, 288, 30, 32);
        m_Rect[1] = new Rectangle(215, 288, 30, 32);
        m_Raw = new int[m_Logo.getHeight()*m_Logo.getWidth()];
        m_Alpha = 255;
    }
    
    public void Update (float _Time){
        switch(m_Status){
            case 0:
                m_TimeUpdate += _Time;
                if(m_TimeUpdate >= 1.0f){
                    m_TimeUpdate = 0;
                    m_Status = 1;
                }
                break;
            case 1:
                m_Alpha -= 1;
                if(m_Alpha < 0){
                    m_Alpha = 0;
                    m_TimeUpdate += _Time;
                    if(m_TimeUpdate >= 2.0f){
                        m_TimeUpdate = 0;
                        m_Status = 2;
                    }
                }
                break;         
        }
    }
    public void PointerPressed(int x, int y){
       if(m_Status == 2){
            for(int i = 0; i<2; i++){
                if(m_Rect[i].Intersects(new Point(x,y))){
                    if(i==0){
                        //RSAudio.getRS().PlayBubble();
                        RSAudio.getRS().setEnable(true);
                        m_Play.setNextState(
                              new State_Menu(m_Play));
                    }else if(i==1){
                        //RSAudio.getRS().PlayBubble();
                        RSAudio.getRS().setEnable(false);
                        m_Play.setNextState(
                              new State_Menu(m_Play));
                    }
                }
            }
       }
    }
    public void KeyPressed (int _Keycode){
        if(m_Status == 2){
            if(_Keycode == -6 || _Keycode == 49){
                //RSAudio.getRS().PlayBubble();
                RSAudio.getRS().setEnable(true);
                m_Play.setNextState(
                      new State_Menu(m_Play));
            }
            if(_Keycode == -7 || _Keycode == 51){
                //RSAudio.getRS().PlayBubble();
                RSAudio.getRS().setEnable(false);
                m_Play.setNextState(
                      new State_Menu(m_Play));
            }
        }
    }
    
    public void Render (){
        super.Render();
        m_Graphics.setColor(0, 0, 0);
        m_Graphics.fillRect(0, 0, 240, 320);
        switch(m_Status){
            case 1:
                Blend(m_Raw, m_Alpha);
                m_Graphics.drawImage(m_Logo, 75, 115, Graphics.TOP | Graphics.LEFT);
                m_Graphics.drawImage(m_Logo.createRGBImage(m_Raw, m_Logo.getWidth(), m_Logo.getHeight(), true),
                        75, 115, Graphics.TOP | Graphics.LEFT);
                break;
            case 2:
                RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "Enable Sound?", 75, 150, m_Graphics);
                RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "Yes?", 5, 304, m_Graphics);
                RSFont.getRS().getFont(Global.FONT_SMALL).DrawString(
                        "No?", 215, 304, m_Graphics);
        }
    }
    
    public void Blend(int[] _Raw, int _AlphaValue) { 
        int Lenght = _Raw.length;  
        for(int i=0; i<Lenght; i++){ 
            int a = 0; 
            int color = (_Raw[i] & 0x00FFFFFF);
            a = _AlphaValue;
            a = (a<<24); 
            color += a; 
            _Raw[i] = color; 
        } 
    }
    public void Destroy (){

    }
}
